//package smartt.styy.auth.advice;
//
//import lombok.extern.slf4j.Slf4j;
//import smartt.styy.auth.util.RSAEncrypt;
//
//import org.apache.commons.codec.binary.Base64;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.core.MethodParameter;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpInputMessage;
//import org.springframework.http.converter.HttpMessageConverter;
//import org.springframework.web.bind.annotation.ControllerAdvice;
//import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
//
//import java.io.BufferedReader;
//import java.io.ByteArrayInputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.InputStreamReader;
//import java.lang.reflect.Type;
//import java.security.interfaces.RSAPrivateKey;
//
///**
//* 请求数据解密
//*/
//
//@ControllerAdvice(basePackages = "smartt.styy.auth.controller")
//@Slf4j
//public class MyRequestBodyAdvice implements RequestBodyAdvice {
//
//	@Value("${ras-keystore.privateKey}")
//	private String privateKey;
//	
//	@Override
//	public boolean supports(MethodParameter methodParameter, Type targetType,
//			Class<? extends HttpMessageConverter<?>> converterType) {
//		//这里设置成false 它就不会再走这个类了
//		return true;
//	}
//
//	@Override
//	public HttpInputMessage beforeBodyRead(HttpInputMessage request, MethodParameter parameter, Type targetType,
//			Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
//		StringBuilder stringBuilder = new StringBuilder();
//        BufferedReader bufferedReader = null;
//        try {
//            //这个request其实就是入参 可以从这里获取流
//            //入参放在HttpInputMessage里面  这个方法的返回值也是HttpInputMessage
//            InputStream inputStream = request.getBody();
//            if (inputStream != null) {
//                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
//                char[] charBuffer = new char[128];
//                int bytesRead = -1;
//                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
//                    stringBuilder.append(charBuffer, 0, bytesRead);
//                }
//            } else {
//                stringBuilder.append("");
//            }
//        } catch (IOException ex) {
//            throw ex;
//        } finally {
//            if (bufferedReader != null) {
//                try {
//                    bufferedReader.close();
//                } catch (IOException ex) {
//                    throw ex;
//                }
//            }
//        }
//        //获取请求数据
//        String requestStr= stringBuilder.toString();
//        /*****************进行解密start*******************/
//        log.info("【接受的请求数据】string={}",requestStr);
//        
//        // 私钥解密过程
//        byte[] res = null;
//        try {
//        	RSAPrivateKey rsaPrivateKey = RSAEncrypt.loadPrivateKeyByStr(privateKey);
//			res = RSAEncrypt.encrypt(rsaPrivateKey, requestStr.getBytes());
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//        String decode = new String(res);
//        
//        log.info("【解密后的请求数据】decode={}",decode);
//
//        //把数据放到我们封装的对象中
//        return new MyHttpInputMessage(request.getHeaders(), new ByteArrayInputStream(decode.getBytes("UTF-8")));
//	}
//
//	@Override
//	public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
//			Class<? extends HttpMessageConverter<?>> converterType) {
//		return body;
//	}
//
//	@Override
//	public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
//			Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
//		return body;
//	}
//	
//	
//	//这里实现了HttpInputMessage 封装一个自己的HttpInputMessage
//    static class MyHttpInputMessage implements HttpInputMessage {
//        HttpHeaders headers;
//        InputStream body;
//
//        public MyHttpInputMessage(HttpHeaders headers, InputStream body) {
//            this.headers = headers;
//            this.body = body;
//        }
//
//        @Override
//        public InputStream getBody() throws IOException {
//            return body;
//        }
//
//        @Override
//        public HttpHeaders getHeaders() {
//            return headers;
//        }
//    }
//	
//}
